<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Bank;
use app\common\model\User as UserModel;
use app\common\tool\Result;
use think\Response;
use think\response\Json;
use app\common\model\Draw as DrawModel;
use think\exception\ValidateException;

class Draw extends Base
{

    /**
     * 提现规则
     * @param int $type
     * @return Response
     */
    public function rule($type = 1) :Response
    {
        $result['draw_account'] = [
            [
                'title' => '余额',
                'type' => 'currency',
                'balance' => $this->user->assets->currency
            ]
        ];
        $result['draw_factor'] = [
            'draw_open' => $this->system['draw_open'],
            'draw_cost' => $this->system['draw_cost'],
            'draw_bs' => $this->system['draw_bs'],
            'draw_min' => $this->system['draw_min'],
            'draw_msg' => handleRichText($this->system['draw_msg']),
        ];
        $result['user_method'] = [
            [
                'type' => 'alipay',
                'mode' => '支付宝',
                'name' => $this->user->alipay_name,
                'image' => handleImg($this->user->alipay_image),
            ],
            [
                'type' => 'wechat',
                'mode' => '微信',
                'name' => $this->user->wechat_name,
                'image' => handleImg($this->user->wechat_image),
            ],
            [
                'type' => 'bank',
                'mode' => '银行卡',
                'bank_name' => $this->user->bank_name,
                'bank_open' => $this->user->bank_open,
                'bank_account_name' => $this->user->bank_account_name,
                'bank_number' => $this->user->bank_number,
            ],
        ];
        return Result::success('success', $result);
    }

    /**
     * 提现
     * @return Response
     */
    public function save(): Response
    {
        $param = $this->request->post('', '', $this->filter);
        if (empty($param['mode'])) {
            return Result::fail('请选择提现方式');
        }
        try {
            $this->validate($param, 'Draw.'.$param['mode']);
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }

        return Result::fastReturn(DrawModel::add($param, $this->user, $this->system));
    }

    /**
     * 提现记录
     * @return Response
     */
    public function record(): Response
    {
        $map = ['uid' => $this->user->id, 'is_del' => 0, 'type'=>$this->request->get('type', 1)];

        return Result::fastReturn(DrawModel::getPageList($map, 'id,account,mode,money,cost,amount,reject_msg,create_time,status', $this->getPage(), $this->getLimit(), false));
    }

    /**
     * 保存提现方式
     * @param $type
     * @return \think\Response|Json
     * type  提现方式：wechat  alipay bank
     * name  真实姓名（提现方式为微信，支付宝方式时必填）
     * image 收款二维码（提现方式为微信，支付宝方式时必填）
     * bank_name 银行名称（提现方式为银行卡时必填）
     * bank_account_name 银行户名（提现方式为银行卡时必填）
     * bank_number 银行卡号（提现方式为银行卡时必填）
     */
    public function methodSave($type)
    {
        try {
            $param = $this->request->post('', '', $this->filter);

            $v = $type == 'bank' ? 'MethodBank' : 'MethodImg';
            try {
                $this->validate($param, $v);
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }
            switch ($type) {
                case "bank":
                    break;
                default:
                    $param[$type . '_name'] = $param['name'];
                    $param[$type . '_image'] = $param['image'];
                    unset($param['name']);
                    unset($param['image']);
            }

            UserModel::update($param, ['id' => $this->user->id]);

            return Result::success('操作成功');

        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }

    /**
     * 提现方式列表
     * @return Response
     */
    public function methodList() :Response
    {
        $mode = [
            ['name' => '支付宝', 'type' => 'alipay'],
            ['name' => '微信', 'type' => 'wechat'],
            ['name' => '银行卡', 'type' => 'bank']
        ];
        $result = ['code'=>1, 'msg'=>'success', 'data'=>$mode];
        return Result::fastReturn($result);
    }

    /**
     * 提现方式信息
     * @param $type
     * @return Response
     */
    public function methodInfo($type) :Response
    {
        switch ($type) {
            case "alipay":
            case "wechat":
                $name = $type . '_name';
                $image = $type . '_image';
                $result['name'] = $this->user->$name;
                $result['image'] = handleImg($this->user->$image);
                break;
            case "bank":
                $result['bank_name'] = $this->user->bank_name;
                $result['bank_open'] = $this->user->bank_open;
                $result['bank_account_name'] = $this->user->bank_account_name;
                $result['bank_number'] = $this->user->bank_number;
                //$result['bank_list'] = explode(PHP_EOL, $this->system['bank_list']);
                break;
            default:
                return Result::fail('加载失败');

        }

        return Result::fastReturn(['code'=>1, 'msg'=>'success', 'data'=>$result]);
    }

    /**
     * 银行列表
     * @return Response
     */
    public function bankList(): Response
    {
        $list = Bank::where('status', 1)->field('id,title,code')->select();
        $result = ['code'=>1, 'msg'=>'success', 'data'=>$list];

        return Result::fastReturn($result);
    }
}

